<?php
namespace ThirdLogin;

use think\Session;

class qq extends Login
{

    const GET_AUTH_CODE_URL = "https://graph.qq.com/oauth2.0/authorize";
    const GET_ACCESS_TOKEN_URL = "https://graph.qq.com/oauth2.0/token";
    const GET_OPENID_URL = "https://graph.qq.com/oauth2.0/me";

    private $appid = '';
    private $appkey = '';
    private $token = '';
    private $openid = '';
    private $state = '';

    public $APIMap = array(
        /*                       qzone                    */
        "get_user_info" => array(
            "https://graph.qq.com/user/get_user_info",
            array("format" => "json"),
            "GET"
        ),
    );


    private $errorRow = array(
        30001 => '返回状态不匹配',
        50001 => '可能是服务器无法请求https协议',
    );

    function __construct($config)
    {
        foreach ($config as $key => $value) {
            $this->$key = $value;
        }
    }

    public function view()
    {
        $scope = implode(",", array_keys($this->APIMap));


        //-------生成唯一随机串防CSRF攻击
        // $state = md5(uniqid(rand(), TRUE));
        // session('qq_state', $state);

        //-------构造请求参数列表
        $keysArr = array(
            "response_type" => "code",
            "client_id" => $this->appid,
            "redirect_uri" => $this->callback,
            "state" => $this->state?$this->state:'',
            "scope" => $scope
        );

        //根据参数生成url
        $url = $this->createUrl(self::GET_AUTH_CODE_URL, $keysArr);
        header("Location:$url");
        exit;
    }


    public function getUrl()
    {
        $scope = implode(",", array_keys($this->APIMap));


        //-------生成唯一随机串防CSRF攻击
        // $state = md5(uniqid(rand(), TRUE));
        // session('qq_state', $state);



        //-------构造请求参数列表
        $keysArr = array(
            "response_type" => "code",
            "client_id" => $this->appid,
            "redirect_uri" => $this->callback,
            "state" => $this->state?$this->state:'',
            "scope" => $scope
        );

        //根据参数生成url
        $url = $this->createUrl(self::GET_AUTH_CODE_URL, $keysArr);
        return $url;
    }

    //获取token
    private function getToken()
    {
        // $state = session('qq_state');
        // session('qq_state', null);
//        if ($_GET['state'] != $state) {
//            $this->getError(30001);
//            return false;
//        }

        //-------请求参数列表
        $keysArr = array(
            "grant_type" => "authorization_code",
            "client_id" => $this->appid,
            "redirect_uri" => urlencode($this->callback),
            "client_secret" => $this->appkey,
            "code" => $_GET['code']
        );
        $tokenUrl = $this->createUrl(self::GET_ACCESS_TOKEN_URL, $keysArr);

        if (!$response = $this->get_contents($tokenUrl)) {
            return false;
        }

        if (strpos($response, "callback") !== false) {
            $lpos = strpos($response, "(");
            $rpos = strrpos($response, ")");
            $response = substr($response, $lpos + 1, $rpos - $lpos - 1);
            $msg = json_decode($response, true);

//            if ($msg['error']) {
//                $this->getError($msg['error']);
//                return false;
//            }
        }
        $res = [];
        if (strstr($response, 'error')) {
            return false;
        }
        parse_str($response, $res);


        $this->token = $res['access_token'];
        return true;

    }


    //获取openid
    public function getOpenid()
    {

        if (!$this->getToken()) {
            return false;
        }
        $keysArr = array(
            "access_token" => $this->token
        );


        $graph_url = $this->createUrl(self::GET_OPENID_URL, $keysArr);
        $response = $this->get_contents($graph_url);

        if (strpos($response, "callback") !== false) {
            $lpos = strpos($response, "(");
            $rpos = strrpos($response, ")");
            $response = substr($response, $lpos + 1, $rpos - $lpos - 1);
        }
        $res = json_decode($response, true);


        if (array_key_exists('error', $res)) {
            return false;
        }
        $this->openid = $res['openid'];
        return $res['openid'];
    }


    //创建Url
    public function createUrl($baseURL, $keysArr)
    {
        //组成URL
        $combined = $baseURL . "?";
        $valueArr = array();

        foreach ($keysArr as $key => $val) {
            $valueArr[] = "$key=$val";
        }

        $keyStr = implode("&", $valueArr);
        $combined .= ($keyStr);

        return $combined;
    }


    public function getUserInfo($openid = null)
    {
        if (!$openid) {
            $openid = $this->openid;
        }

        $baseUrl = $this->APIMap['get_user_info'][0];
        $keysArr = array(
            'oauth_consumer_key' => $this->appid,
            'access_token' => $this->token,
            'openid' => $openid,
            'format' => 'json'
        );
        // $res = $this->postData($baseUrl, $keysArr);
        $res = $this->getData($baseUrl,$keysArr);
        if (!$res) {
            return false;
        }

        $res = json_decode($res, true);

        $res = $this->create($res);
        return $res;
    }


    public function create($info)
    {
        $res['sex'] = 0;
        if (isset($info['gender']) && $info['gender'] == '男') {
            $res['sex'] = 1;
        } else {
            $res['sex'] = 0;
        }
        $res['nickname'] = $info['nickname'];
        $res['birthday'] = $info['year'] ? $info['year'] . "-01-01" : "2016-01-01";
        $res['headimgurl1'] = $info['figureurl_qq_2'];
        $res['headimgurl2'] = $info['figureurl_qq_2'];
        $res['city'] = $info['city'];
        $res['province'] = $info['province'];
        return $res;
    }


    public function getData($url, $keysArr)
    {
        $combined = $this->createUrl($url, $keysArr);
        return $this->get_contents($combined);
    }


}
